/*
'------ Description Start -------
	'	This javascript is used to create a dhtml drop down menu based on the mouse event (such as onclick, onmouseover and onmouseout) etc.
	'	or specific page
	'	Author: Ken Yim 
	'   Begin Version: 1.0 - July 25, 2005
					   1.1 - September 7, 2005
					   			- Bug fixing
'------ Description End -------

'------ Global Start -------
	g_dropdown_menu_layerClassNameRemoveAddList - used to store the added or removed style class name for an element (button). These values will be used to restore back the default class to the elemeent.
'------ Global End ---------

'------ Public function Start -----
	function isLayerDisplay(layerName) - return TRUE if the style.visibility of a layer is visible. Otherwise return FALSE
	function MM_showHideLayers(param 1, param 2, param 3, param 4, param 5, param 6)
			param 1 - giving a list of layer name to process separated by comma e.g. layer1,layer2 etc.
			param 2 - assign SHOW or HIDE value to 1 or layers
			param 3 - a list of remove class name for drop-down menu button separate by space
			param 4 - a list of add class name for drop-down menu button separate by space
			param 5 - boolean value to specify if other layers should keep opened or not (true/false)
			param 6 - a list of page display the menu, separated by comma e.g. page1.asp,page2.asp etc.
			param 7 - current page which include the menu (the page should have a character "/" before the file name.
															e.g. /default.asp)
'------ Public function End -----

'------ Note start ------
	- Layer name must start at "Layer_" + 'a' to 'z' (max of 26 layers) e.g. Layer_a
	- Button or link name must start at "Layer_" + 'a' to 'z' + "_button" e.g. Layer_a_button
	- param 7 can use "<%=request.ServerVariables("SCRIPT_NAME")%>" to get the current page (ASP)
	- param 2 will be ignore if param 6 is specified
'------ Note end --------

*/
/******* Sample code #1 - display sub menu when onMouseOver ********/
/* Note #1: MM_showHideLayers (param 1, param 2, param 3, param 4, param 5) 
<style type="text/css">
	.rightArrowGreen {background: url("images/arrow_green.gif") no-repeat left 1px; padding-left: 10px; margin-left: 5px;}
	.rightArrowDownGreen {background: url("images/arrow_down_green.gif") no-repeat left 2px; padding-left: 15px;}
	.dropdownMenu {position:relative; z-index:1; left: 0px; top: 0px; border: 1px none #000000; visibility: hidden; display: none;}			
</style>

<a href="xxx" class="rightArrowBrown"
	onMouseOver="javascript:MM_showHideLayers('Layer_a','show','rightArrowGreen','rightArrowDownGreen',true)"
	id="Layer_a_button">Demo</a>
<div id="Layer_a" class="dropdownMenu">
	....
</div>
*/

/******* Sample code #2 - display sub menu when onMouseClick ********/
/* Note #1: MM_showHideLayers (param 1, param 2, param 3, param 4, param 5) 
<style type="text/css">
	.rightArrowGreen {background: url("images/arrow_green.gif") no-repeat left 1px; padding-left: 10px; margin-left: 5px;}
	.rightArrowDownGreen {background: url("images/arrow_down_green.gif") no-repeat left 2px; padding-left: 15px;}
	.dropdownMenu {position:relative; z-index:1; left: 0px; top: 0px; border: 1px none #000000; visibility: hidden; display: none;}			
</style>

<a href="#" class="rightArrowBrown"
	onClick="javascript:(isLayerDisplay('Layer_a'))?MM_showHideLayers('Layer_a','hide','rightArrowDownGreen','rightArrowGreen',true):MM_showHideLayers('Layer_a','show','rightArrowGreen','rightArrowDownGreen',true);"
	id="Layer_a_button">Demo</a>
<div id="Layer_a" class="dropdownMenu">
	....
</div>
*/

/******* Sample code #3 - display sub menu based on specific page ********/
/* Note #1: MM_showHideLayers (param 1, '', '', param 4, param 5, param 6, param 7)
<style type="text/css">
	.rightArrowGreen {background: url("images/arrow_green.gif") no-repeat left 1px; padding-left: 10px; margin-left: 5px;}
	.rightArrowDownGreen {background: url("images/arrow_down_green.gif") no-repeat left 2px; padding-left: 15px;}
</style>

<a href="link_1" class="rightArrowGreen marginLeft10 block" id="Layer_a_button">Menu</a></td>
<div id="Layer_a">
	....
</div>
<script type="text/javascript">
	MM_showHideLayers('Layer_a','','rightArrowGreen','rightArrowDownGreen','','subpage1.asp,subpage2.asp','<%=request.ServerVariables("SCRIPT_NAME")%>');
</script>
*/

var g_dropdown_menu_layerClassNameRemoveAddList="";

function isLayerDisplay(layerName) //Public function
{
	if (obj=MM_findObj(layerName)){
		if (obj.style.visibility =='' || obj.style.visibility == 'hidden')
			return false;
		else
			return true;
	}
	else
		return false;
}

function MM_showHideLayers() { //Public function

	var v,r,a,ps,cp,blnPageExisted,obj,args=MM_showHideLayers.arguments;
	if (args[0] != null){
		arrLayer = args[0].split(",");
		v=args[1]; // get value 'show' or 'hide'
		r=args[2]; // a list of remove class name
		a=args[3]; // a list of add class name
		col=args[4]; // boolean value to specify if other layers should keep opened or not (true/false)
		ps=args[5]; // a list of page display the menu, separated by comma
		cp=args[6]; // current page which include the menu
		if (ps!=null) {
			blnPageExisted= isPageExisted(ps, cp);
			v=null;
		}
		if (v!=null || (v==null && blnPageExisted)){
			if (col)
				MM_closeLayersAndButtons("Layer_", 'a', 'z');	
		}
		for (var i=0; i< arrLayer.length;i++){
			if ((obj=MM_findObj(arrLayer[i]))!=null)
			{
				objStyle = obj.style
				if (objStyle) {
					if (v=='show' || blnPageExisted){
						objStyle.visibility = 'visible';
						objStyle.display = 'block';
					}
					else if (v=='hide' || blnPageExisted){
						objStyle.visibility = 'hidden';
						objStyle.display = 'none';
					}
					obj_b = document.getElementById(obj.id + "_button"); // get button object e.g. Layer_a_button
					
					if (obj_b!=null){
						if ((v=='show' || v=='hide') || (v==null && blnPageExisted)){
							obj_b.className=modifyClassName (obj_b, r, a); // change button image
							addToDefaultClassList (obj_b, r, a)
						}
					}
				}
			}
		}
   }
}



function MM_closeLayersAndButtons(NamePrefix, rangeMin, rangeMax)
{	
	var layerName;
	for (i=rangeMin.charCodeAt(0);i<=rangeMax.charCodeAt(0);i++){
		layerName = NamePrefix + String.fromCharCode(i);
		if ((obj=MM_findObj(layerName))!=null){
			if (obj.style){
				backToDefault(document.getElementById(obj.id + "_button"));
				obj=obj.style;obj.visibility='hidden';obj.display='none';
			}
		}
	}
}

function isPageExisted (listOfPages, searchPage)
{
	arrListOfPages = listOfPages.split(',');
	for (var i=0; i < arrListOfPages.length;i++){
		if (arrListOfPages[i] == searchPage.substr(searchPage.lastIndexOf("/")+1, searchPage.length)){
			return true;			
		}
	}
	return false;
}


function modifyClassName (element, classNameRemoveList, classNameAddList)
{
	var blnFound;
	var newClassNameList="";
	currentClassNameList=element.className;
	arrClassNameList = currentClassNameList.split(' ');
	arrClassNameRemoveList = classNameRemoveList.split(' ');
	for (var i=0; i < arrClassNameList.length;i++){
		blnFound=false;
		for (var j=0; j < arrClassNameRemoveList.length;j++){
			if (arrClassNameList[i].indexOf(arrClassNameRemoveList[j])>-1){	
				blnFound=true;
				break;
			}
		}
		if (!blnFound){
			newClassNameList += arrClassNameList[i]+' ';
		}
	}
	if (newClassNameList.length >0)
		newClassNameList = newClassNameList.substr(0, newClassNameList.length);
	if (classNameAddList.length > 0)
		if (newClassNameList.indexOf(classNameAddList) <= 0)
			newClassNameList += ' '+classNameAddList;
	return newClassNameList;
}

function addToDefaultClassList(element, classNameRemoveList, classNameAddList)
{
	var strAdd = element.id + ":" + classNameRemoveList + ":" + classNameAddList;
	if (g_dropdown_menu_layerClassNameRemoveAddList.indexOf(element.id)>-1)
		return 0;
	else
	{
		if (g_dropdown_menu_layerClassNameRemoveAddList == "")
			g_dropdown_menu_layerClassNameRemoveAddList += strAdd;
		else
			g_dropdown_menu_layerClassNameRemoveAddList += "::" + strAdd;
	}
}

function backToDefault(element)
{
	var classNameRemoveList, classNameAddList = "";
	var arrg_dropdown_menu_layerClassNameRemoveAddList = g_dropdown_menu_layerClassNameRemoveAddList.split ("::");
	var arrTmp;
	for (var i=0; i < arrg_dropdown_menu_layerClassNameRemoveAddList.length;i++){
		arrTmp = arrg_dropdown_menu_layerClassNameRemoveAddList[i].split(":");
		if (arrTmp[0].indexOf(element.id)> -1){
			classNameRemoveList=arrTmp[2];
			classNameAddList=arrTmp[1];
		
			element.className=modifyClassName(element, classNameRemoveList, classNameAddList);
			break;
		}
	}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

